package com.lanhe.app.controller.duoyou;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.common.utils.duoyou.DuoYouUtil;
import com.lanhe.common.utils.duoyou.vo.*;
import com.lanhe.member.service.ILhMemberBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 多游-多量
 *
 * @author zhanggl
 * @date 2023年12月04日16:50:30
 */
@RestController
@RequestMapping("/api/duoyou")
public class ApiDuoYouAdsController extends BaseController {

	@Autowired
	private DuoYouUtil duoYouUtil;

	@Autowired
	private ILhMemberBaseService memberBaseService;


	/**
	 * 分页查询所有数据
	 *
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param advertIds  广告ID列表（可不传）多个广告ID以逗号分割
	 * @param type       列表数据类型（默认为all）
	 * @param appChannel 媒体包渠道ID（仅安卓用）
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @author zhanggl
	 * @date 2023年12月04日20:25:24
	 */
	@GetMapping("/list")
	public AjaxResult list(@RequestParam(name = "deviceIds", defaultValue = "") String deviceIds,
	                       @RequestParam(name = "deviceType", defaultValue = "") String deviceType,
	                       @RequestParam(name = "advertIds", defaultValue = "") String advertIds,
	                       @RequestParam(name = "type", defaultValue = "") String type,
	                       @RequestParam(name = "appChannel", defaultValue = "") String appChannel,
	                       @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
	                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
	                       @RequestParam(name = "k", defaultValue = "") String k) throws Exception {
		if (StringUtils.isBlank(deviceIds)) {
			return error("缺少必须参数，系统设备号数组");
		}
		if (StringUtils.isBlank(deviceType)) {
			return error("缺少必须参数，设备类型");
		}
		if (StringUtils.isBlank(k)) {
			String resource = duoYouUtil.getDuoYouList(getLoginUser().getUserKey(), deviceIds, deviceType, advertIds, type, appChannel, pageNum, pageSize);
			AbsListVo absListVo = JSONObject.parseObject(resource, AbsListVo.class);
			if (!Constants.GeneralIntType.TWO_HUNDRED.equals(absListVo.getStatus_code())) {
				return error("返回列表异常");
			}

			return success(absListVo.getData());
		} else {
			String resource = duoYouUtil.getSearch(getLoginUser().getUserKey(), deviceIds, deviceType, pageNum, pageSize, k);
			AbsSearchVo absSearchVo = JSONObject.parseObject(resource, AbsSearchVo.class);
			if (!Constants.GeneralIntType.TWO_HUNDRED.equals(absSearchVo.getStatus_code())) {
				return error("返回搜索列表异常");
			}

			return success(absSearchVo.getData());
		}

	}

	/**
	 * 查询广告详情
	 *
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param advertId   广告ID
	 * @author zhanggl
	 * @date 2023年12月04日20:25:24
	 */
	@GetMapping("/info")
	public AjaxResult info(@RequestParam(name = "deviceIds", defaultValue = "") String deviceIds,
	                       @RequestParam(name = "deviceType", defaultValue = "") String deviceType,
	                       @RequestParam(name = "advertId", defaultValue = "") String advertId) throws Exception {
		if (StringUtils.isBlank(deviceIds)) {
			return error("缺少必须参数，系统设备号数组");
		}
		if (StringUtils.isBlank(deviceType)) {
			return error("缺少必须参数，设备类型");
		}
		if (StringUtils.isBlank(advertId)) {
			return error("缺少必须参数，广告ID");
		}

		String resource = duoYouUtil.getDuoYouInfo(getLoginUser().getUserKey(), deviceIds, deviceType, advertId);
		AbsInfoVo absInfoVo = JSONObject.parseObject(resource, AbsInfoVo.class);
		if (!Constants.GeneralIntType.TWO_HUNDRED.equals(absInfoVo.getStatus_code())) {
			return error("返回详情异常");
		}

		return success(absInfoVo.getData());
	}

	/**
	 * 查询广告详情--任务
	 *
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param advertId   广告ID
	 * @author zhanggl
	 * @date 2023年12月04日20:25:24
	 */
	@GetMapping("/tasks")
	public AjaxResult tasks(@RequestParam(name = "deviceIds", defaultValue = "") String deviceIds,
	                        @RequestParam(name = "deviceType", defaultValue = "") String deviceType,
	                        @RequestParam(name = "advertId", defaultValue = "") String advertId) throws Exception {
		if (StringUtils.isBlank(deviceIds)) {
			return error("缺少必须参数，系统设备号数组");
		}
		if (StringUtils.isBlank(deviceType)) {
			return error("缺少必须参数，设备类型");
		}
		if (StringUtils.isBlank(advertId)) {
			return error("缺少必须参数，广告ID");
		}

		String resource = duoYouUtil.getTasks(getLoginUser().getUserKey(), deviceIds, deviceType, advertId);
		AbsTasksVo absTasksVo = JSONObject.parseObject(resource, AbsTasksVo.class);
		if (!Constants.GeneralIntType.TWO_HUNDRED.equals(absTasksVo.getStatus_code())) {
			return error("返回任务异常");
		}

		return success(absTasksVo.getData());
	}

	/**
	 * 游戏账号数据
	 *
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param advertId   广告ID
	 * @author zhanggl
	 * @date 2024年01月08日17:29:07
	 */
	@GetMapping("/account")
	public AjaxResult account(@RequestParam(name = "deviceIds", defaultValue = "") String deviceIds,
	                          @RequestParam(name = "deviceType", defaultValue = "") String deviceType,
	                          @RequestParam(name = "advertId", defaultValue = "") String advertId) throws Exception {
		if (StringUtils.isBlank(deviceIds)) {
			return error("缺少必须参数，系统设备号数组");
		}
		if (StringUtils.isBlank(deviceType)) {
			return error("缺少必须参数，设备类型");
		}
		if (StringUtils.isBlank(advertId)) {
			return error("缺少必须参数，广告ID");
		}

		AccountVo.DataBean accountVoDate = new AccountVo.DataBean();
		String resource = duoYouUtil.getAccountInfo(getLoginUser().getUserKey(), deviceIds, deviceType, advertId);
		AccountReVo accountReVo = JSONObject.parseObject(resource, AccountReVo.class);
		if (Constants.GeneralIntType.TWO_HUNDRED.equals(accountReVo.getStatus_code())) {
			AccountVo accountVo = JSONObject.parseObject(resource, AccountVo.class);
			accountVoDate = accountVo.getData();
		} else {
			List<AccountVo.DataBean.AccountInfoBean> accountInfoBean = new ArrayList<>();
			accountVoDate.setAccount_info(accountInfoBean);
			accountVoDate.setAccount_info_raw(new AccountVo.DataBean.AccountInfoRawBean());
		}
		return success(accountVoDate);
	}

	/**
	 * 分页查询-参与记录
	 *
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @author zhanggl
	 * @date 2023年12月04日20:25:24
	 */
	@GetMapping("/attends")
	public AjaxResult attends(@RequestParam(name = "deviceIds", defaultValue = "") String deviceIds,
	                          @RequestParam(name = "deviceType", defaultValue = "") String deviceType,
	                          @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
	                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
		if (StringUtils.isBlank(deviceIds)) {
			return error("缺少必须参数，系统设备号数组");
		}
		if (StringUtils.isBlank(deviceType)) {
			return error("缺少必须参数，设备类型");
		}

		String resource = duoYouUtil.getAttends(getLoginUser().getUserKey(), deviceIds, deviceType, pageNum, pageSize);
		AbsAttendsVo absAttendsVo = JSONObject.parseObject(resource, AbsAttendsVo.class);
		if (!Constants.GeneralIntType.TWO_HUNDRED.equals(absAttendsVo.getStatus_code())) {
			return error("返回我参加列表异常");
		}

		return success(absAttendsVo.getData());
	}

	/**
	 * 分页查询-搜索记录
	 *
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @author zhanggl
	 * @date 2023年12月04日20:25:24
	 */
	@GetMapping("/search")
	public AjaxResult getSearch(@RequestParam(name = "deviceIds", defaultValue = "") String deviceIds,
	                            @RequestParam(name = "deviceType", defaultValue = "") String deviceType,
	                            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
	                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
	                            @RequestParam(name = "k", defaultValue = "") String k) throws Exception {
		if (StringUtils.isBlank(deviceIds)) {
			return error("缺少必须参数，系统设备号数组");
		}
		if (StringUtils.isBlank(deviceType)) {
			return error("缺少必须参数，设备类型");
		}

		String resource = duoYouUtil.getSearch(getLoginUser().getUserKey(), deviceIds, deviceType, pageNum, pageSize, k);
		AbsSearchVo absSearchVo = JSONObject.parseObject(resource, AbsSearchVo.class);
		if (!Constants.GeneralIntType.TWO_HUNDRED.equals(absSearchVo.getStatus_code())) {
			return error("返回搜索列表异常");
		}

		return success(absSearchVo.getData());
	}

	/**
	 * 分页查询-推荐列表
	 *
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @author zhanggl
	 * @date 2023年12月04日20:25:24
	 */
	@GetMapping("/recommends")
	public AjaxResult recommends(@RequestParam(name = "deviceIds", defaultValue = "") String deviceIds,
	                             @RequestParam(name = "deviceType", defaultValue = "") String deviceType,
	                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
	                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
		if (StringUtils.isBlank(deviceIds)) {
			return error("缺少必须参数，系统设备号数组");
		}
		if (StringUtils.isBlank(deviceType)) {
			return error("缺少必须参数，设备类型");
		}

		String resource = duoYouUtil.getRecommends(getLoginUser().getUserKey(), deviceIds, deviceType, pageNum, pageSize);
		AbsRecommendsVo absRecommendsVo = JSONObject.parseObject(resource, AbsRecommendsVo.class);
		if (!Constants.GeneralIntType.TWO_HUNDRED.equals(absRecommendsVo.getStatus_code())) {
			return error("返回推荐列表异常");
		}

		return success(absRecommendsVo.getData());
	}


	/**
	 * 推荐列表--BANNERs
	 *
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @param pageNum    页码
	 * @param pageSize   每页返回数据条数（默认10条）
	 * @author zhanggl
	 * @date 2023年12月04日20:25:24
	 */
	@GetMapping("/banners")
	public AjaxResult banners(@RequestParam(name = "deviceIds", defaultValue = "") String deviceIds,
	                          @RequestParam(name = "deviceType", defaultValue = "") String deviceType,
	                          @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
	                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
		if (StringUtils.isBlank(deviceIds)) {
			return error("缺少必须参数，系统设备号数组");
		}
		if (StringUtils.isBlank(deviceType)) {
			return error("缺少必须参数，设备类型");
		}

		String resource = duoYouUtil.getBanners(getLoginUser().getUserKey(), deviceIds, deviceType, pageNum, pageSize);
		AbsRecommendsVo absRecommendsVo = JSONObject.parseObject(resource, AbsRecommendsVo.class);
		if (!Constants.GeneralIntType.TWO_HUNDRED.equals(absRecommendsVo.getStatus_code())) {
			return error("返回推荐列表异常");
		}

		return success(absRecommendsVo.getData());
	}


	/**
	 * 分页查询所有数据
	 *
	 * @param deviceIds  系统设备号数组,格式为JSON
	 * @param deviceType 设备类型
	 * @author zhanggl
	 * @date 2023年12月04日20:25:24
	 */
	@GetMapping("/conf")
	public AjaxResult conf(@RequestParam(name = "deviceIds", defaultValue = "") String deviceIds,
	                       @RequestParam(name = "deviceType", defaultValue = "") String deviceType) throws Exception {
		if (StringUtils.isBlank(deviceIds)) {
			return error("缺少必须参数，系统设备号数组");
		}
		if (StringUtils.isBlank(deviceType)) {
			return error("缺少必须参数，设备类型");
		}

		String resource = duoYouUtil.getConf(getLoginUser().getUserKey(), deviceIds, deviceType);
		ConfVo confVo = JSONObject.parseObject(resource, ConfVo.class);
		if (!Constants.GeneralIntType.TWO_HUNDRED.equals(confVo.getStatus_code())) {
			return error("返回用户信息异常");
		}
		if (ObjectUtil.isNotEmpty(confVo.getData())) {

			memberBaseService.updateDuoyouVipNumber(confVo.getData().get(0).getVip_number() + "", getUserId());
		}
		return success();
	}
}

